package com.wen.wenda.controller;

import com.wen.wenda.common.BaseCode;
import com.wen.wenda.common.BaseResponse;
import com.wen.wenda.common.utils.ReturnUtil;
import com.wen.wenda.exception.BusinessException;
import com.wen.wenda.model.domain.Question;
import com.wen.wenda.model.domain.UserAnswer;
import com.wen.wenda.model.request.answer.UserAnswerCreateRequest;
import com.wen.wenda.model.request.answer.UserAnswerSearchRequest;
import com.wen.wenda.model.request.answer.UserAnswerUpdateRequest;
import com.wen.wenda.model.request.app.AppSearchRequest;
import com.wen.wenda.model.request.question.QuestionUpdateRequest;
import com.wen.wenda.model.vo.QuestionVO;
import com.wen.wenda.model.vo.UserAnswerVO;
import com.wen.wenda.service.UserAnswerService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回答接口
 *
 * @author wen
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {

    @Resource
    private UserAnswerService userAnswerService;

    @PostMapping("/create")
    public BaseResponse<Boolean> createAnswer(@RequestBody UserAnswerCreateRequest userAnswerCreateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (userAnswerCreateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的添加逻辑
        UserAnswer userAnswer = new UserAnswer();
        BeanUtils.copyProperties(userAnswerCreateRequest, userAnswer);
        boolean save = userAnswerService.save(userAnswer);
        return ReturnUtil.success(save);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteAnswer(@RequestBody long id, HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        boolean removedById = userAnswerService.removeById(id);
        if (!removedById) {
            throw new BusinessException(BaseCode.RESOURCE_NOT_FOUND, "删除失败");
        }
        return ReturnUtil.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateAnswer(@RequestBody UserAnswerUpdateRequest userAnswerUpdateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (userAnswerUpdateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的修改逻辑
        UserAnswer userAnswer = userAnswerService.getById(userAnswerUpdateRequest.getId());
        BeanUtils.copyProperties(userAnswerUpdateRequest, userAnswer);
        boolean res = userAnswerService.updateById(userAnswer);
        return ReturnUtil.success(res);
    }

    @GetMapping("/list")
    public BaseResponse<List<UserAnswerVO>> getAnswerList(UserAnswerSearchRequest userAnswerSearchRequest, HttpServletRequest request) {
        if (userAnswerSearchRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的查询校验逻辑
        List<UserAnswer> list = userAnswerService.list();
        List<UserAnswerVO> userAnswerVOList = list.stream().map(item -> {
            UserAnswerVO userAnswerVO = new UserAnswerVO();
            BeanUtils.copyProperties(item, userAnswerVO);
            return userAnswerVO;
        }).toList();
        return ReturnUtil.success(userAnswerVOList);
    }
}
